package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkModuleSettlement;
import com.zb.mapper.ZjNetworkModuleSettlementMapper;
import com.zb.service.ZjNetworkModuleSettlementService;
import com.zb.vo.NetworkModuleSettlementVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点服务模块结算表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Service
public class ZjNetworkModuleSettlementServiceImpl extends ServiceImpl<ZjNetworkModuleSettlementMapper, ZjNetworkModuleSettlement> implements ZjNetworkModuleSettlementService {

    // 新增、修改、删除服务模块结算
    @Override
    public void save(Long custAccId, Long networkId, List<NetworkModuleSettlementVo> moduleSettlements, List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            this.removeByIds(ids);
        }
        if (CollectionUtils.isEmpty(moduleSettlements)) {
            return;
        }
        List<ZjNetworkModuleSettlement> adds = new ArrayList<>();
        List<ZjNetworkModuleSettlement> updates = new ArrayList<>();
        ZjNetworkModuleSettlement settlement = null;
        for (NetworkModuleSettlementVo vo : moduleSettlements) {
            settlement = new ZjNetworkModuleSettlement();
            BeanUtil.copyProperties(vo, settlement);
            if (null != settlement.getId()) {
                updates.add(settlement);
                continue;
            }
            settlement.setId(IdWorker.getId());
            settlement.setCustAccId(custAccId);
            settlement.setNetworkId(networkId);
            adds.add(settlement);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据服务商ID获取服务商结算模块
    @Override
    public List<ZjNetworkModuleSettlement> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "module_id", "price");
        wrapper.eq("network_id", networkId);
        return this.list(wrapper);
    }

    // 根据服务商ID删除服务模板结算
    @Override
    public void deleteBatchByNetworkIds(List<Long> networkIds) {
        if (CollectionUtils.isEmpty(networkIds)) {
            return;
        }
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.in("network_id", networkIds);
        this.remove(wrapper);
    }

    // 根据服务商ID & 模块ID 获取服务商模块结算价
    @Override
    public ZjNetworkModuleSettlement getNetworkIdAndModuleId(Long networkId, Long moduleId) {
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "module_id", "price");
        wrapper.eq("network_id", networkId)
                .eq("module_id", moduleId);
        return this.list(wrapper).stream().findFirst().orElse(null);
    }

    // 根据服务商ID & 模块ID 获取服务商模块结算价
    @Override
    public List<ZjNetworkModuleSettlement> getNetworkIdAndModuleIds(Long networkId, List<Long> moduleIds) {
        if (CollectionUtil.isEmpty(moduleIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select("id", "module_id", "price");
        wrapper.eq("network_id", networkId)
                .in("module_id", moduleIds);
        return this.list(wrapper);
    }

    // 根据模块ID获取与服务商关联的模块ID
    @Override
    public List<Long> getRelationByModuleIds(List<Long> moduleIds) {
        if (CollectionUtil.isEmpty(moduleIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select( "module_id");
        wrapper.in("module_id", moduleIds);
        return this.list(wrapper).stream().map(ZjNetworkModuleSettlement::getModuleId).distinct().toList();
    }

    // 根据服务商ID & 工单模块ID获取服务商结算价
    @Override
    public List<ZjNetworkModuleSettlement> getByNetworkyIdsAndModuleIds(List<Long> networkIds, List<Long> moduleIds) {
        if (CollectionUtil.isEmpty(networkIds) || CollectionUtil.isEmpty(moduleIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkModuleSettlement> wrapper = new QueryWrapper<>();
        wrapper.select( "module_id", "network_id", "price");
        wrapper.in("module_id", moduleIds)
                .in("network_id", networkIds);
        return this.list(wrapper);
    }
}
